package tictactoe.util;

//~--- non-JDK imports --------------------------------------------------------

import tictactoe.Game;
import tictactoe.Player;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Home
 */
public class Score {
    private Player       cross, circle;
    private int          maximumGameCount;
    private List<Result> results;

    /**
     *
     * @param cross
     * @param circle
     * @param maximumGameCount
     */
    public Score(Player cross, Player circle, int maximumGameCount) {
        results               = new ArrayList<Result>();
        this.cross            = cross;
        this.circle           = circle;
        this.maximumGameCount = maximumGameCount;
    }

    /**
     *
     * @param game
     */
    public void add(Game game) {
        if (gamesRemaining() > 0) {
            results.add(new Result(game));
        } else {

            // TODO Exception tree
        }
    }

    /**
     *
     * @return
     */
    public int getCrossPoints() {
        int p = 0;

        for (Result r : results) {
            if (r.getWinner() == Constants.CROSS) {
                p++;
            }
        }

        return p;
    }

    /**
     *
     * @return
     */
    public int getCirclePoints() {
        int p = 0;

        for (Result r : results) {
            if (r.getWinner() == Constants.CIRCLE) {
                p++;
            }
        }

        return p;
    }

    /**
     *
     * @return
     */
    public int getTurnsCount() {
        int t = 0;

        for (Result r : results) {
            t += (r.getTurnsCount() + 1) / 2;
        }

        return t;
    }

    /**
     *
     * @return
     */
    public int gamesPlayed() {
        return results.size();
    }

    /**
     *
     * @return
     */
    public int gamesRemaining() {
        return maximumGameCount - results.size();
    }

    /**
     *
     * @return
     */
    public int getWinner() {
        int x = 0,
            o = 0;

        for (Result r : results) {
            if (r.getWinner() == Constants.CROSS) {
                x++;
            }

            if (r.getWinner() == Constants.CIRCLE) {
                o++;
            }
        }

        if (x == o) {

            // TODO Exception
            return -1;
        }

        return (x > 0)
               ? Constants.CROSS
               : Constants.CIRCLE;
    }

    /**
     *
     * @return
     */
    public Player getWinningPlayer() {
        switch (getWinner()) {
        case Constants.CROSS :
            return cross;

        case Constants.CIRCLE :
            return circle;

        default :

            // TODO Exception
            return null;
        }
    }

    /**
     *
     * @return
     */
    public Player getCross() {
        return cross;
    }

    /**
     *
     * @return
     */
    public Player getCircle() {
        return circle;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
